package com.xingmang.admin.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xingmang.common.constant.PageEntity;
import com.xingmang.common.constant.ResponseData;
import com.xingmang.common.constant.ResultBean;
import com.xingmang.common.dto.admin.OaFlowInfoDTO;
import com.xingmang.common.dto.admin.OaFlowPageDTO;
import com.xingmang.common.dto.admin.OaFlowSaveDTO;
import com.xingmang.common.dto.admin.OaFlowUpdateDTO;
import com.xingmang.common.utils.TokenUtils;
import com.xingmang.common.vo.admin.OaFlowPageVO;
import com.xingmang.db.entity.OaCategory;
import com.xingmang.db.entity.OaFlow;
import com.xingmang.db.service.OaCategoryDBService;
import com.xingmang.db.service.OaFlowDBService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @auther: hansom
 * @Version: 1.0
 * @create 2023/6/30 10:13
 */
@Slf4j
@Service
public class OaFlowService {
    @Resource
    private OaFlowDBService oaFlowDBService;

    @Resource
    private OaCategoryDBService oaCategoryDBService;

    @PostMapping("page")
    public ResultBean<PageEntity<OaFlowPageVO>> page(OaFlowPageDTO dto) {
        Page<OaFlow> page = oaFlowDBService.page(new Page<>(dto.getPagenum(), dto.getPagesize()),
                new LambdaQueryWrapper<OaFlow>()
                        .like(StringUtils.isNotEmpty(dto.getFlowName()), OaFlow::getFlowName, dto.getFlowName())
                        .eq(dto.getCategoryId() != null, OaFlow::getCategoryId, dto.getCategoryId())
                        .orderByDesc(OaFlow::getCreateTime));
        List<OaFlowPageVO> collect = page.getRecords().stream().map(oaFlow -> {
            OaFlowPageVO vo = BeanUtil.copyProperties(oaFlow, OaFlowPageVO.class);
            OaCategory oaCategory = oaCategoryDBService.getById(oaFlow.getCategoryId());
            if (oaCategory != null) {
                vo.setCategoryName(oaCategory.getCategoryName());
            }
            return vo;
        }).collect(Collectors.toList());
        PageEntity<OaFlowPageVO> pageEntity = new PageEntity<>();
        BeanUtil.copyProperties(page,pageEntity);
        pageEntity.setRecords(collect);
        return ResponseData.success(pageEntity);
    }

    public ResultBean save(OaFlowSaveDTO dto) {
        Long userId = TokenUtils.getUserId();
//        Long userId = 1L;
        OaCategory oaCategory = oaCategoryDBService.getById(dto.getCategoryId());
        if (oaCategory==null){
            return ResponseData.error("选择的应用分类已不存在");
        }else{//创建新的应用，需要在对应的应用分类下增加应用数量
            OaFlow one = oaFlowDBService.getOne(new LambdaQueryWrapper<OaFlow>().eq(OaFlow::getFlowName, dto.getFlowName()).eq(OaFlow::getCategoryId, dto.getCategoryId()));
            if (one!=null){
                return ResponseData.error("此分类下的应用名已存在");
            }
            OaFlow oaFlow = BeanUtil.copyProperties(dto, OaFlow.class);
            oaFlow.setUserId(userId);
            oaFlowDBService.save(oaFlow);
            oaCategory.setFlowCount(oaCategory.getFlowCount()+1);
            oaCategoryDBService.updateById(oaCategory);
        }
        return ResponseData.success(null);
    }

    public ResultBean update(OaFlowUpdateDTO dto) {
        OaFlow oaFlow = BeanUtil.copyProperties(dto, OaFlow.class);
        oaFlow.setId(dto.getFlowId());
        oaFlowDBService.updateById(oaFlow);
        return ResponseData.success(null);
    }

    public ResultBean delete(OaFlowInfoDTO dto) {
        OaFlow oaFlow = oaFlowDBService.getById(dto.getFlowId());
        oaFlowDBService.removeById(dto.getFlowId());
        OaCategory oaCategory = oaCategoryDBService.getById(oaFlow.getCategoryId());
        oaCategory.setId(oaFlow.getCategoryId());
        oaCategory.setFlowCount(oaCategory.getFlowCount()-1);
        oaCategoryDBService.updateById(oaCategory);
        return ResponseData.success(null);
    }
}
